<?php

require_once('php/lib/mvc/controller.php');
config('SearchController', 'search_controller');

	class SearchController extends Controller {
		
		var $amountmb, $amountaddr, $user_id, $test, $email,  $failmessage;
		var $check = true;
				
		function init() {
			$this->test = false;
			# initialize data sources
			require_once('php/databases/helios_database.php');
			require_once('php/models/helios.php');
			require_once('php/views/yapter_view.php');
			$this->helios_database =& new heliosDatabase();
			$this->helios = new Helios($this->helios_database);			
		}
		
		/**
		 * Standard function when nothing else was called. This function will call viewsettings()
		 * 
		 */
		function view() {
			$view = new YapterView('content/home.html');
			return $view;
		}
			
		/**
		 * View email settings
		 * 
		 */
		function doSearch() {
			$view = new YapterView('content/dosearch.tpl');
			$resultcount = 0;
						
			if (($_POST) || (strlen($_GET['searchline']) > 0 ))
			{
				$searchline = $_POST ? $_POST['value'] : $_GET['searchline'];
				$searchvalue = $_POST ? $_POST['search'] : $_GET['searchvalue'];
				
				if (strlen($searchline) < 1) {
					$view->replace('results', 'nosearch');
					
					$view->set('searchline', '');
					$view->set('searchvalue', '');
					$view->set('previous', '');
					$view->set('next', '');
				} else {				
					$view->set('searchline', $searchline);
					$view->set('searchvalue', $searchvalue);
					
					if ($searchvalue == "file")
					{
						$rows = $this->helios->getFile($searchline);
						
						$resultcount = count($rows);
						if ($resultcount > 30) 
						{
							$view->set('previous', 'previous');
							$view->set('next', '<a href="?search&value=red&page=1">next</a>');
						} else {
							$view->set('previous', 'previous');
							$view->set('next', 'next');
						}
						
						if (!empty($rows))
						{
							foreach ($rows as $row)
							{
								$view->replace('results','file');
								$view->set('resultname', $row['name']);
								$view->set('resultdetail', $this->helios->getFolderFromId($row['parent']));
								$view->parse('results');
							}
						} else {
							$view->replace('results', 'noresults');
						}
					}
					else if ($searchvalue == "folder")
					{
						$rows = $this->helios->getFolder($searchline);
						
						$resultcount = count($rows);
						if ($resultcount > 30) 
						{
							$view->set('previous', 'previous');
							$view->set('next', '<a href="?search&value=red&page=1">next</a>');
						} else {
							$view->set('previous', 'previous');
							$view->set('next', 'next');
						}
						
						if (!empty($rows))
						{
							foreach ($rows as $row)
							{
								$view->replace('results','folder');
								$view->set('resultname', $this->helios->getFolderFromId($row['id']));
								$view->parse('results');
							}
						} else {
							$view->replace('results', 'noresults');
						}
					}
					else if ($searchvalue == "track")
					{
						$rows = $this->helios->getTrack($searchline);
						
						$resultcount = count($rows);
						if ($resultcount > 30) 
						{
							$view->set('previous', 'previous');
							$view->set('next', '<a href="?search&value=red&page=1">next</a>');
						} else {
							$view->set('previous', 'previous');
							$view->set('next', 'next');
						}
						
						if (!empty($rows))
						{
							foreach ($rows as $row)
							{
								// FIXME: album
								$artist = $this->helios->getArtistById($row['artist']);
								$album = $this->helios->getAlbumById($row['album']);;
								
								$view->replace('results','track');
								$view->set('resultname', $row['title']);
								$view->set('artist', $artist['name']);
								$view->set('album', $album['title']);
								$view->parse('results');
							}
						} else {
							$view->replace('results', 'noresults');
						}
					}
					else if ($searchvalue == "artist")
					{
						$rows = $this->helios->getArtist($searchline);
						
						$resultcount = count($rows);
						if ($resultcount > 30) 
						{
							$view->set('previous', 'previous');
							$view->set('next', '<a href="?search&value=red&page=1">next</a>');
						} else {
							$view->set('previous', 'previous');
							$view->set('next', 'next');
						}
						
						if (!empty($rows))
						{
							foreach ($rows as $row)
							{
								$albums = $this->helios->getAlbumsFromArtist($row['id']);
								
								$view->replace('results', 'artist');
								$view->set('resultname', $row['name']);
									foreach ($albums as $album) {
										$view->set('album', $album['title']);
										$view->parse('albums');
									}
								$view->parse('results');
							}
						} else {
							$view->replace('results', 'noresults');
						}
					}
					else if ($searchvalue == "album")
						{
						$rows = $this->helios->getAlbum($searchline);
						
						$resultcount = count($rows);
						if ($resultcount > 30) 
						{
							$view->set('previous', 'previous');
							$view->set('next', '<a href="?search&value=red&page=1">next</a>');
						} else {
							$view->set('previous', 'previous');
							$view->set('next', 'next');
						}
						
						if (!empty($rows))
						{
							foreach ($rows as $row)
							{
								$artist = $this->helios->getArtistById($row['artist']);
								
								$view->replace('results', 'album');
								$view->set('resultname', $row['title']);
								$view->set('artist', $artist['name']);
								$view->parse('results');
							}
						} else {
							$view->replace('results', 'noresults');
						}
					}
				}
			} else {
				$view->replace('results', 'nosearch');
					
				$view->set('searchline', '');
				$view->set('searchvalue', '');
				$view->set('previous', '');
				$view->set('next', '');
			}
			
			$view->set('resultinfo', $resultcount . ' Results');
			
			return $view;
		}
	}	
?>


